package com.easwift.qsp;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.easwift.qsp.das.xgy.TokenInfo;
import com.easwift.qsp.util.BitUtils;
import com.easwift.qsp.util.DataSourceUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @author: huameng
 * @date: 2020年08月06日 10:44
 */
public class Test0001 {

    public static void main(String[] args) throws Exception{
        /*String sql = "select token from tb_token where id =(SELECT max(id) FROM tb_token)";
        String s = DataSourceUtils.getJdbcTemplate().queryForObject(sql, String.class);
        System.out.println("--"+s);

        String sql1 = "select * from tb_token where id =(SELECT max(id) FROM tb_token)";
        TokenInfo info= DataSourceUtils.getJdbcTemplate().queryForObject(sql1, new Object[]{}, new  BeanPropertyRowMapper<TokenInfo>(TokenInfo.class) {});
        System.out.println(info.getToken());*/
/*
        double x=0;
        byte[] bytes = {(byte)0x3E,0x34,(byte)0xCA,(byte)0x3F};
        bytes = BitUtils.reverseArray(bytes);
        byte[] bytes2 = {(byte)0xC2,(byte)0x8C,(byte)0x87,(byte)0x3F};
        bytes2 = BitUtils.reverseArray(bytes2);

         x = BitUtils.byteArrayToFloat(bytes);
        double y = BitUtils.byteArrayToFloat(bytes2);
        System.out.println("x:"+x);
        System.out.println("y:"+y);*/
        String getTokenUrl = "https://open.ys7.com/api/lapp/token/get";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("appKey", "fb60072e3ce0416d844d7f281da699fe");
        param.put("appSecret", "f5f5ddbeb440046c7e6bfd128443cb21");
        String result = HttpUtil.post(getTokenUrl, param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        String token = JSONUtil.parseObj(jsonObject.get("data")).get("accessToken").toString();
        //并保存到数据库
        String sql1 = String.format("insert into tb_token (token,createtime) values (%s,%s)","'"+token+"'","now()");
        DataSourceUtils.getJdbcTemplate().execute(sql1);



    }


}
